package com.zzh.wsss.service.impl;

import com.zzh.wsss.mapper.ApplyOperateMapper;
import com.zzh.wsss.mapper.JobOperateMapper;
import com.zzh.wsss.mapper.UserOperateMapper;
import com.zzh.wsss.pojo.Apply;
import com.zzh.wsss.pojo.Job;
import com.zzh.wsss.pojo.User;
import com.zzh.wsss.service.UserJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserJobServiceImpl implements UserJobService {
    @Autowired
    private JobOperateMapper jobOperateMapper;

    @Autowired
    private UserOperateMapper userOperateMapper;

    @Autowired
    private ApplyOperateMapper applyOperateMapper;

    public List<Job> getJobs(String username, String count) {
        List<Job> list;
        User user=userOperateMapper.getUserMag(username);
        list=jobOperateMapper.getJobs(Integer.parseInt(count));
        for(int i=0;i<list.size();i++){
            Job j=list.get(i);
            if(user.getJobId()!=null&&user.getJobId()==j.getId())
                list.remove(i);
            else {
                Integer userNum = userOperateMapper.getJobUserNum(j.getId());
                if (userNum >= j.getNumber()) list.remove(i);
            }
        }
        return list;
    }

    public Integer getJobNum() {
        return jobOperateMapper.getJobNum();
    }

    public Map<String, Object> userJobApply(String username, String jobId, String action, String info) {
        Map<String,Object> m=new HashMap<String, Object>();
        Integer userId=userOperateMapper.getUserId(username).getId();
        Integer i = applyOperateMapper.getUserApplyNum(userId);
        if(i==0){
            if(info==null)applyOperateMapper.addApplyNull(userId,Integer.parseInt(jobId),Integer.parseInt(action));
            else applyOperateMapper.addApply(userId,Integer.parseInt(jobId),Integer.parseInt(action),info);
            m.put("result",true);
        }else
            m.put("result",false);
        return m;
    }


    public Map<String, Object> getUserJobInfo(String jobId) {
        Map<String,Object> map=new HashMap<String, Object>();
        Job job=jobOperateMapper.getJob(Integer.parseInt(jobId));
        Integer num=userOperateMapper.getJobUserNum(Integer.parseInt(jobId));
        map.put("job",job);
        map.put("jobUserNum",num);
        return map;
    }

}
